<template>
  <Container class="common-list bg-light">
    <Tabs v-model="tab">
      <Button
        size="large"
        class="item"
        type="primary"
        slot="extra"
        icon="md-add"
        @click="onAddPolicy"
      >新增政策</Button>
      <TabPane label="政策限额设置" name="tab1">
        <FormItem :label-width="20">
          <div class="common-class">
            <i class="ivu-icon ivu-icon-ios-alert"></i>
            <span>提示：政策之间如果有规则冲突，以最后修改时间晚的为准。</span>
          </div>
        </FormItem>
        <div class="table-content" v-for="(item,index) in list.data" :key="index">
          <div class="table-describe">
            <span>政策名称: {{item.policyName }}</span>
            <span>生效时间: {{item.beginDate | formatDateFilter}} - {{item.endDate | formatDateFilter}}</span>
            <span>最后修改时间: {{item.modifyDate | formatDateFilter}}</span>
            <Button type="primary" @click="openEdit(item)">编辑</Button>
            <Button type="primary" @click="onDelete(item, index)">删除</Button>
          </div>
          <Table
            stripe
            :columns="list.columns"
            :data='format(item.policyItems)'
            border
            no-data-text="亲，没有找到相关记录哦！~"
          ></Table>
        </div>
        <div class="no-data" v-if="this.list.data.length==0">
          <p>亲,暂时没有任何政策哦！</p>
        </div>
        <Loader :status="status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import fundData from "./list/PolicyLimitsListData";
import { clearEmpty, formatDate ,getOriginData} from "@/util";
import data from "../../operation/codeQuery/list/CodeListData";
export default {
  data() {
    return {
      keyword: "",
      tab: "tab1",
      status: "pass",
      list: fundData,
      columnData: []
    }
  },
  filters: {
    formatDateFilter(v) {
      return formatDate(v);
    },
  },
  mounted() {
    this.getData();
  },
  destroyed() {
    this.$bus.$off();
  },

  methods: {
    onAddPolicy() {
      this.$router.push("/policy/policyLimits/edit");
    },
    openEdit(item) {
      this.$router.push({
        path: "/policy/policyLimits/edit",
        query: { id: item.id },
      });
    },
    getData() {
      let param = {
        policyName: this.keyword,
      };
      param = clearEmpty(param);
      this.status = "loading";
      this.$ajaxGet("/setting/purchase_quantity_policy/page", param)
        .then((data) => {
          this.list.data = [];
          this.list.data = data || [];
          this.status = "pass";
        })
        .catch(() => {
          this.list.status = "error";
        });
    },
    format (list) {
      var newData = []
      list.forEach(listItem => {
        var flag = newData.some(item => item.storeTypeId === listItem.storeTypeId)
        if (flag) {
          var index = newData.findIndex(item => item.storeTypeId === listItem.storeTypeId)
          var itemData = newData[index]
          itemData.brandList.push({
            brandName: listItem.brandName,
            brandId: listItem.brandId
          })
          itemData.taskQuantity.push(listItem.taskQuantity)
        } else {
          var obj = {}
          obj.storeTypeName = listItem.storeTypeName
          obj.storeTypeId = listItem.storeTypeId
          obj.maxIntegralQuantity = listItem.maxIntegralQuantity
          obj.brandList = []
          obj.taskQuantity = []
          obj.brandList.push({
            brandName: listItem.brandName,
            brandId: listItem.brandId
          })
          obj.taskQuantity.push(listItem.taskQuantity)
          newData.push(obj)
        }
      })
      return newData
    },
    onDelete(item, index) {
      let { id, policyName: name } = item;
      this.$Modal.confirm({
        title: "删除",
        content: "确定删除当前政策？",
        onOk: () => {
          this.$ajaxPost(`/setting/purchase_quantity_policy/delete/${id}`).then(
            (data) => {
              this.$Notice.success({
                title: "删除成功",
                desc: `<span class="font-red">${name}</span> 已被删除。`,
              });
              this.list.data.splice(index, 1);
            }
          );
        },
      });
    },
  },
};
</script>
